package ar.edu.unicen.exa.isistan.archassis.control;

import java.util.List;

import ar.edu.unicen.exa.isistan.archassis.diff.LastVersionDiffStrategy;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.model.ModelManager;
import ar.edu.unicen.exa.isistan.archassis.model.persistence.PersistenceManager;
import ar.edu.unicen.exa.isistan.archassis.rcs.RCSException;
import ar.edu.unicen.exa.isistan.archassis.source.ISourceStrategy;


public class ControlManager {
	private static ControlManager instance = new ControlManager();
	private ControlManager() {}
	public static ControlManager getInstance() {
		return instance;
	}
	public List<JavaClass> process2() throws RCSException {
		ISourceStrategy sourceStrategy = ModelManager.getInstance().getSourceStrategy();
		List<JavaClass> result = PersistenceManager.getCacheDiff(sourceStrategy);
		
		if (result == null) {
			result = getDiff(sourceStrategy);
			PersistenceManager.setCacheDiff(sourceStrategy, result);
		}
		return result;
	}
	public void process() throws RCSException {
		ISourceStrategy sourceStrategy = ModelManager.getInstance().getSourceStrategy();
		List<JavaClass> result = PersistenceManager.getCacheDiff(sourceStrategy);
		
		if (result == null) {
			result = getDiff(sourceStrategy);
			PersistenceManager.setCacheDiff(sourceStrategy, result);
		}
		
		ModelManager.getInstance().setClassesList(result);
	}
//	private void process2() throws RCSException {
////		IRevisionControlSystem rcs = ModelManager.getInstance().getRevisionControlSystem();
////		IVersion versionFrom = ModelManager.getInstance().getVersionFrom();
////		IVersion versionTo = ModelManager.getInstance().getVersionTo();
//		
//		Map<String, List<JavaClass>> result = PersistenceManager.getCacheDiff(rcs, versionFrom, versionTo);
//		
//		if (result == null) {
//			result = getDiff(rcs, versionFrom, versionTo);
//			PersistenceManager.setCacheDiff(rcs, versionFrom, versionTo, result);
//		}
//		
//		ModelManager.getInstance().setSRCFolderClassesMap(result);
//		//ModelManager.getInstance().setClassesList(la);
//
//	}
	
	private static List<JavaClass> getDiff(ISourceStrategy ss) {
		return ss.getJavaClassList(new LastVersionDiffStrategy());
	}
//	private static File getCache() {
//		IRevisionControlSystem rcs = ModelManager.getInstance().getRevisionControlSystem();
//		IVersion versionFrom = ModelManager.getInstance().getVersionFrom();
//		IVersion versionTo = ModelManager.getInstance().getVersionTo();
//		
//		DiffRequestData tempFile = rcs.getDiffRequestData();
//		return new File(tempFile.getRootTempPath(), "cache_" + versionFrom.getID() + "-" + versionTo.getID() + ".bin");
//
//	}
	
//	private void setCacheDiff(Map<String, List<JavaClass>> result) {
//		File cacheFile = getCache();
//		
//		try {
//			ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream(cacheFile));
//			fileOut.writeObject(result);
//			fileOut.close();
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//		}
//	}
	
//	private static Map<String, List<JavaClass>> getCacheDiff() {
//		File cacheFile = getCache();
//		if (!cacheFile.isFile()) {
//			return null;
//		}
//		
//		Map<String, List<JavaClass>> result = null;
//		
//		try {
//
//			ObjectInputStream fileIn = new ObjectInputStream(new FileInputStream(cacheFile));
//			result = (Map<String,List<JavaClass>>)fileIn.readObject();
//			fileIn.close();
//		} catch (Exception e) {
//			// TODO: handle exception
//			System.err.println(e.getMessage());
//		}
//		return result;
//	}
//	private static Map<String, List<JavaClass>> getDiff(IRevisionControlSystem rcs, IVersion versionFrom, IVersion versionTo) throws RCSException {
//		
//		//RCSFacade.getInstance().download(rcs, versionFrom, versionTo);
//		RCSFileDiffList fileDiffList = rcs.getDiff(versionFrom, versionTo);
//		
//		DiffRequestData tempFile = rcs.getDiffRequestData();
//		
//		
//		
//		//List<RCSFileDiff> listFD0 = Filter.filtrar(RCSFileModification.ADDED, listFD);
//		List<RCSFileDiff> listFD0 = fileDiffList.remove(RCSFileModification.ADDED);
//		rcs.checkOut(versionFrom, listFD0, tempFile);
//		
//		//List<RCSFileDiff> listFD1 = RCSFileDiffListFilter.filtrar(RCSFileModification.DELETED, listFD);
//		List<RCSFileDiff> listFD1 = fileDiffList.remove(RCSFileModification.DELETED);
//		rcs.checkOut(versionTo, listFD1, tempFile);
//		
//		ClassResolver classResolver = new ClassResolver(tempFile.getDestinationFor(versionTo));
//		List<IAction> srcIActionList = JavaDiffFacade.compare(tempFile, versionFrom, versionTo, classResolver);
//		
//		Map<String, List<JavaClass>> srcJavaClassMap = new HashMap<String, List<JavaClass>>();
//		
//		List<JavaClass> lax = NewClassEclipseActionPrint.filtrar(srcIActionList);
//			
//		ItemCase caso = new ItemCase(lax, "....");
//		List<ItemCase> listC = new ArrayList<ItemCase>();
//		listC.add(caso);
//		ListCases listaCasos = new ListCases("", "", listC, "Descripcion ....");
//		
//		srcJavaClassMap.put("root", lax);
//		
//		return srcJavaClassMap;
//	}
}
